package com.rainner.admin.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.admin.action.base.EasyuiAction;
import com.rainner.admin.defined.PermissionDefined;
import com.rainner.admin.defined.SystemDefine;
import com.rainner.admin.javabean.ManagerBean;
import com.rainner.admin.server.ManagerServer;
import com.rainner.admin.server.RolePermissionServer;
@Results({
		@Result(name = "ManagePeople", type = "freemarker", location = "/WEB-INF/content/manage/operator.ftl")
})
public class SystemManageAction extends EasyuiAction {

	private static final long serialVersionUID = 1L;
	private String type;
	private ManagerServer server = new ManagerServer();
	private ManagerBean bean = new ManagerBean();
	private RolePermissionServer permissionServer = new RolePermissionServer();
	/**
	 * 管理操作员页面
	 * 
	 * @return
	 */
	@Action("/manage/operator")
	public String manageOperator() {
		// 获取主界面需要的数据,存放到map中.
		// 获取操作员权限并判断
		Object username = ServletActionContext.getRequest().getSession().getAttribute("username");
		if(username != null){
			return "ManageOperator";
		}
		return null;
	}
	/**
	 * 管理增删改查操作
	 * 
	 * @return
	 * @throws Exception 
	 */
	@Action("/manage/operator" + "/{type}")
	public String manageOperatorType() throws Exception {
		Object username = ServletActionContext.getRequest().getSession().getAttribute("username");
		if (SystemDefine.OPERATOR_INIT.equals(type) && permissionServer.checkPermission(PermissionDefined.SEARCH_OPERATOR, username.toString())) {
			map.put("total", server.manageDataCount());
			map.put("rows", server.manageData(rows, page));
		} else if (SystemDefine.OPERATOR_ADD.equals(type) && permissionServer.checkPermission(PermissionDefined.ADD_OPERATOR, username.toString())) {
			boolean state = server.manageAddData(bean.getManagerName(), bean.getManagerPassword(), bean.getManagerRemark());
			if(state){
				super.setResult(state, "新增操作员操作完成.");
			}else{
				super.setResult(state, "新增操作员操作失败.");
			}
			map.put("message", result);
		} else if (SystemDefine.OPERATOR_DELETE.equals(type) && permissionServer.checkPermission(PermissionDefined.DELETE_OPERATOR, username.toString())) {
			boolean state = server.manageDelData(bean.getManagerName());
			if(state){
				super.setResult(state, "删除操作员操作完成.");
			}else{
				super.setResult(state, "删除操作员操作失败.");
			}
			map.put("message", result);
		} else if (SystemDefine.OPERATOR_UPDATE.equals(type) && permissionServer.checkPermission(PermissionDefined.UPDATE_OPERATOR, username.toString())) {
			HttpSession session = ServletActionContext.getRequest().getSession();
			if(server.updataPassword(session.getAttribute("username").toString(), bean.getManagerPassword())){
				this.result.setMsg("修改操作员操作成功");
			}else{
				this.result.setMsg("修改操作员操作失败");
			}
			map.put("message", result);
		}
		return MAP;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public ManagerBean getBean() {
		return bean;
	}

	public void setBean(ManagerBean bean) {
		this.bean = bean;
	}
	
}
